use PlaceEntityActionTestHelper;
use MoveActionTestHelper;
use ResourceManager;
use Map;
use ActionManager;
use MapManager;
use MapLoader;

# Se colocan 2 entidades en el mapa
# una se mueve referenciandola en forma directa
# la otra referenciando su tipo bien conocido

my $rm = ResourceManager->new();
my $am = $rm->getComponent("ActionManager");

my $mapLoader = MapLoader->new({WIDTH=>10, HEIGHT=>10});
my $map = $mapLoader->load;

my $e = $rm->getComponent("EntityManager")->spawnEntity("Knight", "UNREGISTERED");

my ($x, $y) = (3, 6);
my $params = {MAP=>$map, ENTITY=>$e, X=>$x, Y=>$y, CATEGORY=>"STUFF"};
my $action = $am->newAction("PlaceEntity", $params);
$action->perform;

# ============================
my ($x2, $y2) = (1, 1);
my $wellKnownEntity = "KING";
$king = $rm->getComponent("EntityManager")->spawnEntity("Knight", $wellKnownEntity);

$params = {MAP=>$map, ENTITY=>$king, X=>$x2, Y=>$y2, CATEGORY=>$wellKnownEntity};
$action = $am->newAction("PlaceEntity", $params);
$action->perform;

# ============================

print "Posicion INICIAL:\n";
drawMap($map);
## Ya hay una entidad colocada en el mapa

#---------------------------------------------------------------
#---------------------------------------------------------------

# PROBAR MOVIENDO REFERENCIANDO LA ENTIDAD DIRECTAMENTE
# =======================================================

$params = {
    ENTITY=> $e,
    MAP=> $map,
    DIRECTION => "UP"
};
$action = $am->newAction("Move", $params);

testParameters($action, $params);
print "\n";

testValidate($action);
print "\n";

# Mover
$action->perform;

testEntityPosition($e, $map, $x, $y-1);
print "\n";

testMovementToTile($e, $map, $x, $y-1);
print "\n";

print "(***MUST FAIL***)\n";
testMovementToTile($e, $map, $x, $y);
print "\n";


# PROBAR MOVIENDO REFERENCIANDO EL TIPO BIEN CONOCIDO
# ¡Y SIN REFERENCIAR EL MAPA!
# =======================================================
$rm->getComponent("MapManager")->registerMap($map, "DEFAULT");
$rm->getComponent("MapManager")->select("DEFAULT");

$params = {
    WELL_KNOWN_ENTITY=> "KING",
    DIRECTION => "LEFT"
};
$action = $am->newAction("Move", $params);

testParameters($action, $params);
print "\n";

testValidate($action);
print "\n";

# Mover
$action->perform;

testEntityPosition($king, $map, $x2-1, $y2);
print "\n";

testMovementToTile($king, $map, $x2-1, $y2);
print "\n";

print "(***MUST FAIL***)\n";
testMovementToTile($king, $map, $x2, $y2);
print "\n";



print "Estado Final\n";
drawMap($map);
